#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : Moty
# @Time     : 2024/1/22 0:06
# @File     : kc16_01.py
# @Project  : jt808
def parse_01_login_auth(body):
    # 解析0x01 充电桩登录认证
    data = {
        "桩编码": body[0:14],
        "桩类型": check_charging_pile_type(body),
        "充电枪数量": int(body[16:18], 16),
        "通信协议版本": "云快充 (" + str(parse_protocol_version(int(body[18:20], 16)/10)) + ")",
        "程序版本": bytes.fromhex(body[20:36]).decode("ascii") + "(" + body[20:36] + ")",
        "网络链接类型": parse_network_type(int(body[36:38], 16)) + "(" + body[36:38] + ")",
        "Sim 卡": body[38:58],
        "运营商": str(parse_telecom_operators(int(body[58:60], 16)) + '(' + body[58:60]) + ')',
    }
    formatted_data = ""
    for key, value in data.items():
        formatted_data += f"{key}: {value}\n"
    return formatted_data


def parse_telecom_operators(result_value):
    results = ['移动', '电信', '联通', '其他']
    if 0 <= result_value < len(results):
        return results[result_value]
    else:
        return '未知结果'


def parse_protocol_version(protocol_version):
    if protocol_version == 1.6:
        return protocol_version
    elif protocol_version == 1.5:
        return protocol_version
    else:
        return '未知版本'


def check_charging_pile_type(body):
    # 将 body[14:16] 转换为整数，并以16进制解析
    result = int(body[14:16], 16)
    # 判断结果并返回对应的类型
    if result == 0:
        return "直流桩" + body[14:16]
    elif result == 1:
        return "交流桩" + body[14:16]
    else:
        return "未知桩类型" + body[14:16]


def parse_network_type(result_value):
    results = ['SIM 卡', 'LAN', 'WAN', '其他']
    if 0 <= result_value < len(results):
        return results[result_value]
    else:
        return '未知网络'

__all__ = ['parse_01_login_auth']
